package com.gs.synedocket.dialog;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gs.synedocket.R;


public class ToastDialog extends BaseDialog {

    private TextView tvMsg;
    private ImageView ivIco;
    private String msg;

    private int resId;
    private Context context;
    private AnimationDrawable mAnimation;

    public ToastDialog(@NonNull Context context) {
        super(context, R.style.toast_dialog);
        this.context = context;
    }

    public ToastDialog setIco(int resId) {
        this.resId = resId;
        return this;
    }

    public ToastDialog setMessage(String msg) {
        this.msg = msg;
        return this;
    }

    @Override
    public void refreshView() {
        tvMsg.setText(msg);
        if (resId > 0) {
            ivIco.setImageResource(resId);

            if (resId == R.mipmap.loading) {
//                ivIco.setImageResource(R.drawable.round_spinner_fade);
//                mAnimation = (AnimationDrawable) ivIco.getDrawable();
                Animation anim = AnimationUtils.loadAnimation(context, R.anim.progressbar);
                anim.start();
                ivIco.startAnimation(anim);
            }
        } else {
            ivIco.setVisibility(View.GONE);
        }
    }

//    @Override
//    public void onWindowFocusChanged(boolean hasFocus) {
//        super.onWindowFocusChanged(hasFocus);
//        if (mAnimation != null) {
//            ivIco.post(()-> mAnimation.start());
//        }
//    }

    @Override
    public void initView() {
        tvMsg = findViewById(R.id.tvMsg);
        ivIco = findViewById(R.id.ivIco);
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_toast;
    }


}
